﻿using SysSurv.bll;
using SysSurv.dtl;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
using System.Windows.Forms;

namespace SysSurv
{
    public partial class frmFillSurvey : Form
    {
        private static int intQ;
        private static int cont;
        public frmFillSurvey()
        {
            InitializeComponent();
        }

        private void frmFillSurvey_Load(object sender, EventArgs e)
        {
            try
            {
                lblSurveyName.Text = Wiii.getInstance().srvAux.strName;
                intQ = Wiii.getInstance().srvAux.lstQuestions.Count;
                cont = 0;
                showQA();
                Wiii.getInstance().us = new UserSurvey();
                Wiii.getInstance().us.srvSurvey = Wiii.getInstance().srvAux;
                Wiii.getInstance().us.usrUser = Wiii.getInstance().UsrLog;
                Wiii.getInstance().us.lstSelAnswers = new List<Answer>();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void showQA(){
            try
            {
                lblQuestion.Text = Wiii.getInstance().srvAux.lstQuestions.ElementAt(cont).strQuestion;
                lstAnswers.DataSource = Wiii.getInstance().srvAux.lstQuestions.ElementAt(cont).lstAnswers;
                btnControl();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void btnControl() {
            if (cont + 1 == intQ)
            {
                btnNext.Hide();
                btnEnd.Show();
            }
            else
            {
                btnNext.Show();
                btnEnd.Hide();
            }
        }


        private void btnNext_Click(object sender, EventArgs e)
        {
            if (lstAnswers.SelectedIndex >= 0)
            {
                try
                {
                    Wiii.getInstance().us.lstSelAnswers.Add((Answer)lstAnswers.SelectedItem);
                    cont += 1;
                    showQA();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else {
                MessageBox.Show("Debe seleccionar una respuesta");
            }
        }

        private void btnEnd_Click(object sender, EventArgs e)
        {
            if (lstAnswers.SelectedIndex >= 0)
            {
                try
                {
                    Wiii.getInstance().us.lstSelAnswers.Add((Answer)lstAnswers.SelectedItem);
                    string json = new JavaScriptSerializer().Serialize(Wiii.getInstance().us);
                    RestClient client = new RestClient("http://localhost:8080/SpringSurv/surveys/fill", HttpVerb.POST, json, false);
                    client.MakeRequest();
                    this.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
            {
                MessageBox.Show("Debe seleccionar una respuesta");
            }
        }
    }
}
